package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.DemandListBean;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.GetDemandDetailBean;
import com.fy.fyzf.bean.IsInfoBean;
import com.fy.fyzf.bean.MyRecommendBean;
import com.fy.fyzf.bean.ReleaseBean;
import com.fy.fyzf.glideUtils.GlideUtils;
import com.fy.fyzf.presenter.DemandPresenter;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.DemandCallBack;
import com.fy.fyzf.weight.SlideRecyclerView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.adapter.MydemandDetailAdapter;

public class MyDemandDetailActivity extends BaseActivity<DemandPresenter> implements DemandCallBack {


    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_peer_schme)
    TextView tvPeerSchme;
    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.tv_response_count)
    TextView tvResponseCount;
    @BindView(R.id.recycleView)
    SlideRecyclerView recycleView;
    @BindView(R.id.tv_close)
    TextView tvClose;
    private DemandListBean.ListBean data;
    private MydemandDetailAdapter mydemandDetailAdapter;

    @Override
    protected DemandPresenter createPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_my_demand_detail;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("需求详情");
        data = (DemandListBean.ListBean) getIntent().getSerializableExtra("data");

        mydemandDetailAdapter = new MydemandDetailAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mydemandDetailAdapter);
        mydemandDetailAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<DemanddetailBean.PublisherDetailsReleaseVoBean.ListBeanX> data = adapter.getData();
                if (view.getId() == R.id.tv_sign) {

                } else if (view.getId() == R.id.tv_look) {
                    Intent intent = new Intent(MyDemandDetailActivity.this, HouseDetailActivity.class);
                    intent.putExtra("housingid", data.get(position).getHousingId());
                    intent.putExtra("type", 3);
                    intent.putExtra("data", (Serializable) data.get(position));
                    startActivity(intent);

                }
            }
        });
    }

    private GetDemandDetailBean commonSearchBean = new GetDemandDetailBean();

    @Override
    public void initData() {
        super.initData();
        commonSearchBean.setIsMy(true);
        commonSearchBean.setReleaseId(data.getReleaseId());
        commonSearchBean.setPageSize(100);
        mPresenter.getDamendDetail(commonSearchBean);
    }

    @Override
    public void getDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void issueDemandSuccess(BaseData data) {

    }

    @Override
    public void getAreaListSuccess(List<AreaListBean> listBeans) {

    }

    @Override
    public void getDamendDetailSuccess(DemanddetailBean demanddetailBean) {
        if (demanddetailBean.getPublisherDetailsReleaseVo().getList().size() == 0) {
            mydemandDetailAdapter.setEmptyView(R.layout.layout_nodata_common,recycleView);
        }
        mydemandDetailAdapter.setNewData(demanddetailBean.getPublisherDetailsReleaseVo().getList());
        showData(data);
    }

    @Override
    public void getMyDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void deleteDemandSuccess(BaseData data, int i) {
        finish();
        EventBus.getDefault().post("issueSuccess");
    }

    @Override
    public void getMyRecommendSuccess(MyRecommendBean myRecommendBean) {

    }

    @Override
    public void isInfoSuccess(IsInfoBean isInfoBean) {

    }

    private ReleaseBean releaseBean = new ReleaseBean();
    @OnClick({R.id.iv_head, R.id.tv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                break;
            case R.id.tv_close:
                releaseBean.setReleaseId(data.getReleaseId());
                mPresenter.deleteDemand(releaseBean,0);
                break;
        }
    }

    private void showData(DemandListBean.ListBean data) {
        String logo = SPUtils.getInstance(this).getString("logo", "");
        String name = SPUtils.getInstance(this).getString("name", "");
        GlideUtils.loadCircleImage(logo, ivHead);
        tvName.setText(name);
        String company = SPUtils.getInstance(this).getString("company", "");
        tvCompany.setText(company);
        List<String> list = new ArrayList<>();
        list.add(data.getAreaName());
        list.add(data.getReleaseArea() + "m²");
        list.add(data.getReleaseBudget() + "元/天");

        switch (data.getRenovationType()) {
            case 0:
                list.add("装修不限");
                break;
            case 1:
                list.add("精装");
                break;
            case 2:
                list.add("简装");
                break;
            case 3:
                list.add("豪装");
                break;
            case 4:
                list.add("无装修");
                break;
        }

        switch (data.getReleaseType()) {
            case 1:
                tvType.setText("写字楼");
                break;
            case 2:
                tvType.setText("商铺");
                break;
            case 3:
                tvType.setText("厂房");
                break;
            case 4:
                tvType.setText("酒店");
                break;
            case 5:
                tvType.setText("公寓");
                break;
        }
        flowlayout.setAdapter(new TagAdapter<String>(list) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(MyDemandDetailActivity.this).inflate(R.layout.item_peer_tab,
                        flowlayout, false);
                tv.setText(list.get(position));
                return tv;
            }
        });

        if (data.getCooperationProgram() != null) {
            switch (data.getCooperationProgram()) {
                case 1:
                    tvPeerSchme.setText("合作方案: 10%");
                    break;
                case 2:
                    tvPeerSchme.setText("合作方案: 20%");
                    break;
                case 3:
                    tvPeerSchme.setText("合作方案: 30%");
                    break;
                case 4:
                    tvPeerSchme.setText("合作方案: 40%");
                    break;
                case 5:
                    tvPeerSchme.setText("合作方案: 50%");
                    break;
            }
        }

        if (data.getOtherNeeds() != null) {
            tvRemark.setText(data.getOtherNeeds());
        } else {
            tvRemark.setText("暂无其他要求描述，详情请去电咨询。");
        }

        tvResponseCount.setText("已收到" + data.getResponseCount() + "条推荐");
    }
}